package com.example.administrator.ygddriver.appliedorder;

import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.BaseHint;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.listen.ListenAdapter;
import com.example.administrator.ygddriver.listen.ListenRequestBean;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.map.MapDw;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;

import java.util.HashMap;

/**
 * 已申请订单
 */
public class AppliedOrderActivity extends BaseActivity {
    private RecyclerView mRv;
    private ListenAdapter mListenAdapter;
    private SimpleToolbar simpleToolbar;
    private LocationClient mLocationClient = null;
    private MyLocationListener myListener = new MyLocationListener();
    private String c_token, c_driver;

    @Override
    public int initLayout() {
        return R.layout.activity_applied_order;
    }

    @Override
    public void initView() {
        mLocationClient = new LocationClient(this);
        //声明LocationClient类
        mLocationClient.registerLocationListener(myListener);
        //注册监听函数
        initOption();

        simpleToolbar = findViewById(R.id.activity_applied_order_stb);
        mRv = findViewById(R.id.activity_applied_order_rv);
        mListenAdapter = new ListenAdapter(R.layout.item_fragment_listen, null);
        View view1 = LayoutInflater.from(this).inflate(R.layout.item_fragment_listen, null);
//        mListenAdapter.setEmptyView(view1);
        mRv.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        mRv.setAdapter(mListenAdapter);
    }

    @Override
    public void initDate() {
        setSimpleToolbar();
        LiteOrmTools.getInformation(UserBean.class, this, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                if (data.getC_token() != null) {
                    c_token = data.getC_token();
                    c_driver = data.getC_id();
                }

            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });
        mLocationClient.start();

    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("已申请订单");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.u126);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //司机端申请中列表查询
    private void initApplicationWaybillHtml(String c_driver, String c_customerlatitude, String c_customerlongitude, String c_token) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_driver", c_driver);
        hashMap.put("c_customerlatitude", c_customerlatitude);
        hashMap.put("c_customerlongitude", c_customerlongitude);
        hashMap.put("c_token", c_token);
        hashMap.put("c_type", "2");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_DRIVER + UrlValuse.APPLICATION_WAY_BILL, hashMap, ListenRequestBean.class, new OkGoRequests.OnTrue<ListenRequestBean>() {
            @Override
            public void hasData(ListenRequestBean date) {
                if (date.isSuccess()) {
                    mListenAdapter.setNewData(date.getResult());
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(AppliedOrderActivity.this, "提示", date.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<ListenRequestBean>() {
            @Override
            public void noData() {

            }
        });
    }

    public class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
            //以下只列举部分获取经纬度相关（常用）的结果信息
            //更多结果信息获取说明，请参照类参考中BDLocation类中的说明

            double latitude = location.getLatitude();    //获取纬度信息
            double longitude = location.getLongitude();    //获取经度信息
            float radius = location.getRadius();    //获取定位精度，默认值为0.0f

            String coorType = location.getCoorType();
            //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准

            int errorCode = location.getLocType();
            //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
            if (errorCode == 161 || errorCode == 61) {
                initApplicationWaybillHtml(c_driver, String.valueOf(latitude), String.valueOf(longitude), c_token);
                mLocationClient.stop();
            } else {
//                BaseHint baseHint = new BaseHint.addHint().setParameter(AppliedOrderActivity.this, "提示", "定位失败");
//                baseHint.show();
                Log.d("MyLocationListener", "定位失败");
            }

        }
    }

    private void initOption() {
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        option.setCoorType("bd09ll");
        option.setScanSpan(1000);
        option.setOpenGps(true);
        option.setLocationNotify(true);
        //可选，设置是否当GPS有效时按照1S/1次频率输出GPS结果，默认false
        option.setIgnoreKillProcess(true);
        //可选，定位SDK内部是一个service，并放到了独立进程。
        //设置是否在stop的时候杀死这个进程，默认（建议）不杀死，即setIgnoreKillProcess(true)
        option.SetIgnoreCacheException(false);
        //可选，设置是否收集Crash信息，默认收集，即参数为false
        option.setWifiCacheTimeOut(5 * 60 * 1000);
        //可选，V7.2版本新增能力
        //如果设置了该接口，首次启动定位时，会先判断当前Wi-Fi是否超出有效期，若超出有效期，会先重新扫描Wi-Fi，然后定位
        option.setEnableSimulateGps(false);
        //可选，设置是否需要过滤GPS仿真结果，默认需要，即参数为false
        mLocationClient.setLocOption(option);
    }

}
